package com.stroe.util;

import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Map.Entry;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class JsonUtil {
	public static String ListToJsonArray(@SuppressWarnings("rawtypes") List list) {
		StringBuffer result = new StringBuffer();
		JSONArray array = JSONArray.fromObject(list);
		for (int i = 0; i < array.size(); i++) {
			StringBuffer sb = new StringBuffer();
			JSONArray array2 = array.getJSONArray(i);
			for (int j = 0; j < array2.size(); j++) {
				JSONObject object = array2.getJSONObject(j);
				@SuppressWarnings("unchecked")
				Set<Entry<String, Object>> set = object.entrySet();
				Iterator<Entry<String, Object>> iterator = set.iterator();
				while (iterator.hasNext()) {
					Entry<String, Object> entry = iterator.next();
					sb.append("\"").append(entry.getKey()).append("\"")
							.append(":").append("\"").append(entry.getValue())
							.append("\"").append(",");

				}
			}
			sb.deleteCharAt(sb.length() - 1);
			sb.insert(0, "{").insert(sb.length(), "}");
			result.append(sb.toString()).append(",");

		}
		result.deleteCharAt(result.length() - 1);
		result.insert(0, "[");
		result.insert(result.length(), "]");
		return result.toString();
	}
}
